Jump to content

User:Ramesh Ramaiah/useridentifier.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* If you want to use this script, simply add the following line to your monobook.js:
 
importScript('User:Anomie/useridentifier.js'); // Linkback: [[User:Anomie/useridentifier.js]]
 
* (Please keep the comment so I can see how many people use this).
*/
 
var UserIdentifier={
    images:{
        '**':'//upload.wikimedia.org/wikipedia/commons/thumb/7/77/Wikipedia_svg_logo.svg/20px-Wikipedia_svg_logo.svg.png',
        'bot':'//upload.wikimedia.org/wikipedia/commons/thumb/5/5d/Crystal_Clear_action_run.png/20px-Crystal_Clear_action_run.png',
        'sysop':'//upload.wikimedia.org/wikipedia/commons/thumb/e/e0/Mop.svg/20px-Mop.svg.png',
        'bureaucrat':'//upload.wikimedia.org/wikipedia/commons/thumb/a/ac/Human-preferences-desktop.svg/20px-Human-preferences-desktop.svg.png',
        'checkuser':'//upload.wikimedia.org/wikipedia/commons/thumb/1/1e/Gnome-searchtool.svg/20px-Gnome-searchtool.svg.png',
        'reviewer':'//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Wikipedia_Reviewer.svg/20px-Wikipedia_Reviewer.svg.png',
        'steward':'//upload.wikimedia.org/wikipedia/commons/thumb/7/75/Wikimedia_Community_Logo.svg/20px-Wikimedia_Community_Logo.svg.png',
        'accountcreator':'//upload.wikimedia.org/wikipedia/commons/thumb/5/5a/Wikipedia_Accountcreators_v2.svg/20px-Wikipedia_Accountcreators_v2.svg.png',
        'oversight':'//upload.wikimedia.org/wikipedia/commons/thumb/0/06/Oversight_logo.png/20px-Oversight_logo.png',
        'rollbacker':'//upload.wikimedia.org/wikipedia/commons/thumb/d/df/Wikipedia_Rollback.svg/20px-Wikipedia_Rollback.svg.png'
    },
 
    make_callback:function(name){
        var users=null;
        var lastedit=null;
        return function(r){
            if(!r.query) throw new Error('Bad response');
            if(r.query.users){
                if(r.query.users.length!=1) throw new Error('Bad response');
                users=r.query.users[0];
            }
            if(r.query.usercontribs){
                lastedit=r.query.usercontribs.length?r.query.usercontribs[0].timestamp:'never';
            }
            if(!users || !lastedit) return;
            r=users;
            if(r.name!=name) return;
            r.emailable=(typeof(r.emailable)!='undefined');
            var d=document.createElement('DIV');
            var d2=document.createElement('DIV');
            d2.style.position='relative';
            d2.style.cssFloat='left';
            d2.style.styleFloat='left';
            d2.style.overflow='visible';
            d2.style.cursor='pointer';
            d2.style.zIndex=101;
            d2.className='metadata';
            d2.title='Registered: '+r.registration+'\nLast edit: '+lastedit+'\nEdits: '+r.editcount+'\nEmailable: '+(r.emailable?'yes':'no')+'\nGender: '+r.gender;
            d2.onclick=function(){ d.style.display=d.style.display?'':'none'; };
            d.style.display='none';
            d.style.position='absolute';
            d.style.background='#eef';
            d.style.border='1px solid black';
            d.style.padding='3px';
            d.style.zIndex=1000;
            d.appendChild(document.createTextNode('Registered: '+r.registration));
            d.appendChild(document.createElement('BR'));
            d.appendChild(document.createTextNode('Last edit: '+lastedit));
            d.appendChild(document.createElement('BR'));
            d.appendChild(document.createTextNode('Edits: '+r.editcount));
            d.appendChild(document.createElement('BR'));
            d.appendChild(document.createTextNode('Emailable: '+(r.emailable?'yes':'no')));
            d.appendChild(document.createElement('BR'));
            d.appendChild(document.createTextNode('Gender: '+r.gender));
            if(r.groups && r.groups.length>0){
                d.appendChild(document.createElement('BR'));
                d.appendChild(document.createTextNode('Groups: '+r.groups.join(', ')));
                for(var j=r.groups.length-1; j>=0; j--){
                    if(UserIdentifier.images[r.groups[j]]){
                        var i=document.createElement('IMG');
                        i.src=UserIdentifier.images[r.groups[j]];
                        i.width=20;
                        i.height=20;
                        i.style.cursor='pointer';
                        i.title=r.groups[j];
                        d2.appendChild(i);
                    }
                }
                d2.title+='\nGroups: '+r.groups.join(', ');
            }
            if(!d2.firstChild){
                var i=document.createElement('IMG');
                i.src=UserIdentifier.images['**'];
                i.width=20;
                i.height=20;
                i.style.cursor='pointer';
                d2.appendChild(i);
            }
            var i=document.createElement('SPAN');
            i.style.fontSize='16px';
            if(r.gender=='male'){
                i.appendChild(document.createTextNode('♂'));
                i.style.color='#89CFF0';
            } else if(r.gender=='female'){
                i.appendChild(document.createTextNode('♀'));
                i.style.color='#F4C2C2';
            }
            if(i.firstChild) d2.insertBefore(i, d2.firstChild);
            d.onclick=function(){ d.style.display='none'; };
            var h=document.getElementsByTagName('H1')[0];
            h.insertBefore(d2,h.firstChild);
            h.parentNode.insertBefore(d,h.nextSibling);
            if(window.CascadeTopicons) CascadeTopicons();
        };
    },
 
    IDUser:function(name){
        mw.loader.using('mediawiki.util', function(){
            name = decodeURIComponent(name).replace(/_/g, ' ');
            var cb=UserIdentifier.make_callback(name);
            $.ajax({
                url:mw.util.wikiScript('api'),
                dataType:'json',
                type:'POST',
                data:{
                    format:'json',
                    action:'query',
                    list:'users',
                    ususers:name,
                    usprop:'editcount|groups|registration|emailable|gender'
                },
                success:cb,
                error:function(xhr,textStatus,errorThrown){
                    throw new Error('AJAX error: '+textStatus+' '+errorThrown);
                }
            });
            $.ajax({
                url:mw.util.wikiScript('api'),
                dataType:'json',
                type:'POST',
                data:{
                    format:'json',
                    action:'query',
                    list:'usercontribs',
                    ucuser:name,
                    ucprop:'timestamp',
                    uclimit:1
                },
                success:cb,
                error:function(xhr,textStatus,errorThrown){
                    throw new Error('AJAX error: '+textStatus+' '+errorThrown);
                }
            });
            mw.util.addPortletLink('p-cactions',mw.util.wikiScript()+'?title=Special%3AContributions&target='+encodeURIComponent(name),'Contribs');
            mw.util.addPortletLink('p-cactions','http://toolserver.org/~quentinv57/sulinfo/'+encodeURIComponent(name)+'?showblocks=1','SUL');
        });
    },
 
    onLoad:function(){
        if(mw.config.get('wgNamespaceNumber')==2 || mw.config.get('wgNamespaceNumber')==3){ 
            var u=mw.config.get('wgTitle');
            var i=u.indexOf('/');
            if(i>0) u=u.substr(0,i);
            UserIdentifier.IDUser(u);
        } else if(mw.config.get('wgCanonicalNamespace')=='Special' && mw.config.get('wgCanonicalSpecialPageName')=='Contributions'){
            var a=document.getElementById('contentSub').getElementsByTagName('A')[0].href;
            var u=a.match(/\/wiki\/User:([^\/]+)/);
            if(u) UserIdentifier.IDUser(u[1]);
            u=a.match(/[?&]title=User:([^&]+)/);
            if(u) UserIdentifier.IDUser(u[1]);
        } else if(mw.config.get('wgCanonicalNamespace')=='Special' && mw.config.get('wgCanonicalSpecialPageName')=='Log'){
            var u=['',document.getElementById('mw-log-user').defaultValue];
            if(!u[1]) u=document.getElementById('mw-log-page').defaultValue.match(/^User:([^\/]+)/);
            if(u) UserIdentifier.IDUser(u[1]);
        }
    }
};
 
$(document).ready(UserIdentifier.onLoad);